#!/bin/sh
#
# fgci-acceleron	Startup script for fgci processes for acceleron
#
# chkconfig: 2345 80 80
# description: fcgi-acceleron
# $Id: fcgi-acceleron.sh 35 2008-12-02 20:58:03Z gabriel.preda $

## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI="/usr/bin/spawn-fcgi"

## ABSOLUTE path to the PHP binary
FCGIPROGRAM="/usr/bin/php-cgi"

## bind to tcp-port on localhost
FCGISOCKET="/var/www/html/e-radical.ro/acceleron/acceleron.socket"

## uncomment the PHPRC line, if you want to have an extra php.ini for this user
## store your custom php.ini in /var/www/fastcgi/fred/php.ini
## with an custom php.ini you can improve your security
## just set the open_basedir to the users webfolder
## Example: (add this line in you custom php.ini)
## open_basedir = /var/www/vhosts/fred/html
##
PHPRC="/var/www/html/e-radical.ro/acceleron"

## number of PHP childs to spawn in addition to the default. Minimum of 2.
## Actual childs = PHP_FCGI_CHILDREN + 1
PHP_FCGI_CHILDREN=5

## number of request server by a single php-process until is will be restarted
PHP_FCGI_MAX_REQUESTS=1000

## IP adresses where PHP should access server connections from
FCGI_WEB_SERVER_ADDRS="127.0.0.1"

# allowed environment variables sperated by spaces
ALLOWED_ENV="PATH USER"

## if this script is run as root switch to the following user
USERID="acceleron"
GROUPID="acceleron"
PROG="acceleron"

##################
################## no config below this line
##################
RETVAL=0
L=`ps --user=$USERID | wc -l`

#################### Source function library
. /etc/rc.d/init.d/functions

start() {
	echo -n $"Starting $PROG: "

	if test x$PHP_FCGI_CHILDREN = x; then
		PHP_FCGI_CHILDREN=5
	fi

	export PHP_FCGI_MAX_REQUESTS
	export FCGI_WEB_SERVER_ADDRS
	export PHPRC

	ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS PHPRC"

	# copy the allowed environment variables
	E=

	for i in $ALLOWED_ENV; do
		E="$E $i=$(eval echo "\$$i")"
	done
	# clean environment and set up a new one
	env - $E $SPAWNFCGI -s $FCGISOCKET -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN
	RETVAL=$?

	chmod 777 $FCGISOCKET

	if [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$PROG
	then
		echo_success
	else
		echo_failure
	fi
	echo -e "";

	return $RETVAL
}

stop() {
        echo -n $"Stopping $PROG: "

        killall --user $USERID
        RETVAL=$?
        rm -f $FCGISOCKET

		if [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROG
		then
			echo_success
		else
			echo_failure
		fi
		echo -e "";

        return $RETVAL
}

stat() {
	let SHOULD=$PHP_FCGI_CHILDREN+2;

	if [ "$L" == "$SHOULD" ]
	then
		echo -e $"There are $PHP_FCGI_CHILDREN processes of $0 running under $USERID.";
	else
		echo -e $"Unable to find any $USERID fcgi processes. :(";
	fi
}


#################### Possible commands

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                start
                ;;
        status)
				stat
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|status}"
                RETVAL=1
esac

exit $RETVAL
